import { routes } from "@/router/routes";
import { useAppStore } from "@/stores/app";
import wechatOa from "./wechat";
import { useUserStore } from "@/stores/user";
import { paramsToStr } from "@/utils/util";
import { SHARE_CODE } from "@/enums/cacheEnums";
import { USER_ID,ISBUY_ID,GOODS_ID} from "@/enums/cacheEnums";
import cache from "@/utils/cache";
export default {
	async onShareAppMessage(res : any) {
      
		const { getWebsiteConfig } = useAppStore();
     
		// 101 是邀请好友点击分享按钮跳转过来的
		// 102 是分类页面点击分跳转过来的
        	// await getSharePath()
		if (res.from === "menu") {
           
			return {
				title: getWebsiteConfig.name,
				path: await getSharePath(),
			};
		}else{
            if (res.target.id == "101") {
                return {
                    title: getWebsiteConfig.shop_name,
                    path: "pages/goods/index?isBuy_id=" + cache.get(ISBUY_ID)+"&id="+cache.get(GOODS_ID) || null,
                };
            } else if (res.target.id == "102") {
                return {
                    title: getWebsiteConfig.shop_name,
                    path: "pages/category/index?id=" + cache.get(USER_ID) || null,
                };
            } else if(res.target.id == "110"){
                let pid =  uni.getStorageSync('getbtn1id')
                console.log('=============')
                console.log(pid)
                console.log('=============')
                return {
                    title: getWebsiteConfig.shop_name,
                    path: "pages/goods/index?id=" + pid,
                    imageUrl:'http://mzd-admin.muzhandao.com/api/uploads/image/20241102/c47ace66-f5d9-493e-b9ef-fd5889e3c95c.png'
                };
            }
        }
		
	
	},
	// 分享到朋友圈
	onShareTimeline() {
		const { getWebsiteConfig } = useAppStore();
		return {
			title: getWebsiteConfig.shop_name,
			path: "/pages/index/index",
			imageUrl: "",
		};
	},
	onShow() {
		const menuList = [
			"menuItem:share:appMessage",
			"menuItem:share:timeline",
			"menuItem:share:qq",
			"menuItem:share:weiboApp",
			"menuItem:share:QZone",
		];
		const pageList = getCurrentPages();
		const currentPage = pageList[pageList.length - 1].route;
		const pageMate = routes.find((item) => {
			return item.path == `/${currentPage}`;
		});
		if (pageMate.unshare == true) {
			// #ifdef MP-WEIXIN
			uni.hideShareMenu({
				hideShareItems: [],
			});
			// #endif
			// #ifdef H5
			wechatOa.hideMenuItems(menuList);
			// #endif
		}
	},
	async onLoad(option : any) {
		query = option;
	},
};
let query : any = {};

const getSharePath = async () => {
	const pageList = getCurrentPages();
	const currentPage = pageList[pageList.length - 1].route;
	let origin = "";
	//#ifdef H5
	origin = `${window.location.origin}/mobile`;
	//#endif
	const path = `${origin}/${currentPage}`;

	try {
		const userStore = useUserStore();
		if (!userStore?.userInfo?.code) {
			await userStore.getUser();
		}
		if (userStore?.userInfo?.code) {
			query[SHARE_CODE] = userStore?.userInfo?.code;
		}
	} catch (error) {
		console.log(error);
	}
	console.log(`${path}${paramsToStr(query)}`);

	return `${path}${paramsToStr(query)}`;
};